<template>
  <div class="goods-image">
    <!-- 大图 -->
    <div
      v-show="isShow"
      class="large"
      :style="{
        backgroundImage: `url(${goods[currIndex]})`,
        backgroundPositionX: -2 * left + 'px',
        backgroundPositionY: -2 * top + 'px'
      }"
    ></div>
    <div class="middle" ref="target">
      <img :src="goods[currIndex]" alt="" />
      <!-- 小块块 -->
      <div
        v-show="isShow"
        class="layer"
        :style="{ top: top + 'px', left: left + 'px' }"
      ></div>
    </div>
    <ul class="small">
      <li
        v-for="(item, index) in goods"
        :key="item"
        @mouseenter="currIndex = index"
        :class="{ active: index === currIndex }"
      >
        <img :src="item" alt="" />
      </li>
    </ul>
  </div>
</template>
<script>
/*
1:样式处理（小块块，大图）
2：处理小块块随鼠标移动
    const {elementX,elementY,isOutside} =useMouseInElement(dom)
    elementX:相对于dom移动的x距离 
    elementY:相对于dom移动的y距离 
    isOutside:是否在dom内，dom内:false,外true
3：限制小块块移动范围
4：再处理大图片的移动
5：移出相应区域小块块与大图都消失
*/

import { ref, watch, reactive, toRefs } from 'vue'
import { useMouseInElement } from '@vueuse/core'
export default {
  name: 'GoodsImage',
  props: {
    goods: {
      type: Array,
      default: () => []
    }
  },
  setup (props) {
    const currIndex = ref(0)
    // 小块块随鼠标移动
    const target = ref(null)
    const layerPosition = reactive({
      left: 200,
      top: 0,
      isShow: false
    })
    const { elementX, elementY, isOutside } = useMouseInElement(target)
    // 实时侦听相应值的变化

    watch([elementX, elementY, isOutside], () => {
      layerPosition.isShow = !isOutside.value
      if (!isOutside.value) {
        // 限制不要过左边边界
        layerPosition.left = elementX.value - 100
        layerPosition.top = elementY.value - 100
        if (elementX.value < 100) {
          layerPosition.left = 0
        }
        // 限制不要过右边边界
        if (elementX.value > 300) {
          layerPosition.left = 200
        }
        // 限制上边
        if (elementY.value < 100) {
          layerPosition.top = 0
        }
        // 限制下边
        if (elementY.value > 300) {
          layerPosition.top = 200
        }

        // console.log(elementX.value, elementY.value, isOutside.value)
      }
    })

    return { currIndex, target, ...toRefs(layerPosition) }
  }
}
</script>
<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  z-index: 500;
  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    position: relative;
    cursor: move;
    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0, 0, 0, 0.2);
      left: 0;
      top: 0;
      position: absolute;
    }
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>
